package br.ufmg.dees.insane.ui.rich.prep.view;


import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.plaf.FontUIResource;

import br.ufmg.dees.insane.commons.InsaneProperties;
import br.ufmg.dees.insane.commons.command.CommandHolder;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.commons.view.IView;
import br.ufmg.dees.insane.model.prep.GeoPrepModel;
import br.ufmg.dees.insane.ui.rich.prep.controller.PrepControllerHE;

//  ****************************************************************************
/**
 * This class makes an Intenalframe for a Desktop
 * @author Ferreira, Reginaldo & Pitangueira, Roque
 * @since october 2006
 */
public class InterfacePrep extends JInternalFrame implements ActionListener, Observer {
	
	private static final long serialVersionUID = 1L;
	private PrepView view = new PrepView(this);
    private InsaneProperties properties = new InsaneProperties();
    private NumberFormat numberFormat;
    private DesktopPrep desktop;
    private boolean keyboard_state = false;

	// Panels
    private JPanel panelDraw = new JPanel();
	private JPanel panelToolBar = new JPanel();

   
// *****************************************************************************
    
	/**
	 * The constructor method. <br>
	 * Calls the init() method throwing an exception.
	 */
	public InterfacePrep() {
		super(null);
	    try {
	    	setUpFormat();
	        init();
	       
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}

// *****************************************************************************

	/**
	 *  It's called by the constructor. <br>
	 *  Sets Panels, components and provides a window listener.
	 */
	private void init() throws Exception
	{
        this.validate();
		this.getContentPane().add(panelToolBar,BorderLayout.NORTH);
        this.getContentPane().add(panelDraw, BorderLayout.CENTER);
		this.setFonts();
		this.addInternalFrameListener(new IntFrameListner());
        this.setInitialInterface();
	}
	
//  ****************************************************************************
	
	/** Sets the initial Interface. */
	public void setInitialInterface()
	{

		// Makes the panelToolBar
		panelToolBar.setLayout(new BoxLayout(panelToolBar, BoxLayout.X_AXIS));
		panelToolBar.removeAll();

		// Makes the panelDraw
        panelDraw.setLayout(new BorderLayout());
        panelDraw.setBorder(BorderFactory.createEmptyBorder(0,0,0,3));
        panelDraw.removeAll();
        panelDraw.add(((PrepView)this.view).getDrawingArea(), BorderLayout.CENTER);
        panelDraw.repaint();
        panelDraw.updateUI(); 

        
		// Updates the DrawingArea
		DrawingArea da = ((PrepView)this.view).getDrawingArea();
        da.removeAllCommands();
        da.setCursor();
        da.setClickedCommand(null);
        da.setZoom(((PrepViewState)this.view.getViewState()).getLimits());
		da.setRepainting(true);
		da.setGrid(true);
		da.setSnap(true);
		da.setDistort(false);
		da.recompose();
		da.repaint();
		
		this.repaint();	
	}
	
//  ****************************************************************************	
	/**Sets the fonts used by this InternalInterface
	*/
	public void setFonts() {
		
		try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            
            Font fontCurrent = UIManager.getFont("Button.font");
            String name = fontCurrent.getName();
            
            //Creates similar font with the specified style and size
            FontUIResource fontResourceNew  = new FontUIResource(name, Font.PLAIN, 12);
            
            //Sets all items fonts
            UIManager.put("Button.font", fontResourceNew);
            UIManager.put("CheckBox.font", fontResourceNew);
            UIManager.put("CheckBoxMenuItem.font", fontResourceNew);
            UIManager.put("ColorChooser.font", fontResourceNew);
            UIManager.put("ComboBox.font", fontResourceNew);
            UIManager.put("DesktopIcon.font", fontResourceNew);
            UIManager.put("EditorPane.font", fontResourceNew);
            UIManager.put("InternalFrame.font", fontResourceNew);
            UIManager.put("Label.font", fontResourceNew);
            UIManager.put("List.font", fontResourceNew);
            UIManager.put("MenuBar.font", fontResourceNew);
            UIManager.put("Menu.font", fontResourceNew);
            UIManager.put("MenuItem.font", fontResourceNew);
            UIManager.put("OptionPane.font", fontResourceNew);
            UIManager.put("PasswordField.font", fontResourceNew);
            UIManager.put("PopupMenu.font", fontResourceNew);
            UIManager.put("ProgressBar.font", fontResourceNew);
            UIManager.put("RadioButton.font", fontResourceNew);
            UIManager.put("RadioButtonMenuItem.font", fontResourceNew);
            UIManager.put("ScrollPane.font", fontResourceNew);
            UIManager.put("TabbedPane.font", fontResourceNew);
            UIManager.put("TableHeader.font", fontResourceNew);
            UIManager.put("TextArea.font", fontResourceNew);
            UIManager.put("TextField.font", fontResourceNew);
            UIManager.put("TextPane.font", fontResourceNew);
            UIManager.put("TitledBorder.font", fontResourceNew);
            UIManager.put("ToggleButton.font", fontResourceNew);
            UIManager.put("ToolBar.font", fontResourceNew);
            UIManager.put("ToolTip.font", fontResourceNew);
            UIManager.put("Tree.font", fontResourceNew);
            UIManager.put("Viewport.font", fontResourceNew);

        }
        catch (Exception e){}
	}
	
//  ****************************************************************************	
	
	public void setCoord(String x, String y)
	{
		this.getDesktopFrame().getCoordField().setText("Coords: " + x + ", " + y);
	}	
	
//  ****************************************************************************
	
	/** Returns the properties.
	*@return The properties.
	*/
	public InsaneProperties getProperties() {
		return properties;
	}
	
//  ****************************************************************************	
	
	/** Returns the current controller.
	*  @return The current controller.
	*/
	public IView getView() {
		return this.view;
	}
	
////  ****************************************************************************
//
//	/** Adds a Command to the undoList.
//	 * @param cmd a the desired command
//	 **/
//	public void addToUndo(Command cmd) {
//        undoCmd.add(cmd);
//        itemUndo.setEnabled(true);
//        undo.setEnabled(true);	
//	}
//
////  ****************************************************************************
//
//	/** Adds a Command to the redoList.
//	 * @param cmd a the desired command
//	 */
//	public void addToRedo(Command cmd) {
//        if (!(cmd instanceof DeleteCommand)){
//            redoCmd.add(cmd);
//            itemRedo.setEnabled(true);
//            redo.setEnabled(true);
//        }	
//	}
//
////  ****************************************************************************
//
//	/** Disables the redo command.*/
//	public void nothingToRedo() {
//        redoCmd.clear();
//        itemRedo.setEnabled(false);
//        redo.setEnabled(false);		
//	}
//
////  ****************************************************************************
//
//	/** Disables the undo command.*/
//	public void nothingToUndo() {
//        undoCmd.clear();
//        itemUndo.setEnabled(false);
//        undo.setEnabled(false);		
//	}

//  ****************************************************************************

	/**
	* Sets the Status Bar message.
	* @param str The text to be shown
	*/
	public void setMessage(String str) {
		//this.txf_msg.setText(str);
    }
	
//  ****************************************************************************
	
	/**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(0);
    }
    
//*****************************************************************************

	/** Method which executes the desired action to each command.*/
	public void actionPerformed(ActionEvent evt) {
		CommandHolder obj = (CommandHolder)evt.getSource();
		obj.getCommand().execute();
	}

//	****************************************************************************

	/** Sets the Model.
	*  @param mdl The Model to be set.
	*/
	
	public void setModel(GeoPrepModel mdl)
	{
        ((PrepControllerHE)this.view.getController()).setModel(mdl); 
	}
	
//	****************************************************************************

	/** Gets the Geometric Model. */
	public GeoPrepModel getModel()
	{
		GeoPrepModel mdl;
        mdl = ((PrepControllerHE)this.view.getController()).getModel();
        return mdl;
	}
	
//  ****************************************************************************
	
	/** Returns the current controller.
	*@return The current controller.
	*/
    public PrepControllerHE getController()
    {
        return (PrepControllerHE)view.getController();
    }
    
//  **************************************************************************** 
	/**
	 * Writes the message in this InternalInterface output
	 * @param message The message to be written
	 */
    public void addOutputText(String message)
    {
    	
    }
     
//  ****************************************************************************	
	
    /**
	 * Returns this InternalInterface's DrawingArea
	 * @return this InternalInterface's DrawingArea
	 */
	public DrawingArea getDrawing()
	{
		return null;
	}
	
//  ****************************************************************************  
    
	/** It's called after the model changes */
	public void update(Observable arg0, Object arg1) {
		
		this.getView().getDrawingArea().recompose();
		this.getView().getDrawingArea().repaint();
		this.getModel().setIsModified(true);
		this.setTitle("<+>  " + this.getModel().getName() + " - " + this.getName());
	}	
	
//  ****************************************************************************
	
	public DesktopPrep getDesktopFrame()
	{
		return this.desktop;
	}

//  ****************************************************************************
	
	public void setDesktopFrame(DesktopPrep dtk)
	{
		this.desktop = dtk;
	}
	
//  ****************************************************************************
	
	public void setKeyboardState(boolean st)
	{
		this.keyboard_state = st;
	}
	
//  ****************************************************************************
	
	public boolean isKeyboardOn()
	{
		return this.keyboard_state;
	}	
	

//  ****************************************************************************
	
	public String getIntFrameName()
	{
		return this.getName();
	}
	
//  ****************************************************************************
//  Class InternalFrame Listener	
//  ****************************************************************************	
	
	class IntFrameListner implements InternalFrameListener
	{
		public void internalFrameOpened(InternalFrameEvent e) {}

		public void internalFrameClosing(InternalFrameEvent e) {}

		public void internalFrameClosed(InternalFrameEvent e) {}

		public void internalFrameIconified(InternalFrameEvent e) {}

		public void internalFrameDeiconified(InternalFrameEvent e) {}

		public void internalFrameActivated(InternalFrameEvent e)
		{	
			getDesktopFrame().setStateButtons();
			String cv = " Current view >>   " + getIntFrameName();
			getDesktopFrame().getMsgField().setText(cv);
		}

		public void internalFrameDeactivated(InternalFrameEvent e) {}
		
	}// end of the IntFrameListner
	
//  ****************************************************************************
}//end of this class